﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp11
{
    internal class Monster
    {
        //创建一个私有字段进行实际存储对象 防止外部数据引用报错
        //使用一个公共属性的get方法让外部进行访问 但是不能修改
        private static List<BaseUnit> _lifeMons = new List<BaseUnit>();
        //自定义怪物名称用于随机取名
        //基础属性生命50 攻击力20 防御力3
        //高于基础生命的是强壮   低是胆小  攻击力高是凶猛的   低的软弱的    防御力高 坚硬  低脆弱的
         static   Dictionary<int,string> HealthName=new Dictionary<int, string>()
            {
                {0,"" },{1,"强壮、"},{2,"胆小、"}
            };
        static Dictionary<int, string> AttackName = new Dictionary<int, string>(){
                {0,"" },{1,"凶猛、"},{2,"软弱、"}
            };
        static Dictionary<int, string> DefenceName = new Dictionary<int, string>(){
                {0,"" },{1,"坚硬的"},{2,"脆弱的"}
            };
       static string[] Names = new string[] {"誉宝","小誉宝","大誉宝","陈宝","马誉" };


        /// <summary>
        /// 当前存活的怪物集合
        /// </summary>
        public static List<BaseUnit> LifeMons { get { return _lifeMons; } }

        //已经死亡的怪物集合
        private static Queue<BaseUnit> _deadMons=new Queue<BaseUnit>();
        //定义一个带参数的函数 用于创建怪物并且用于返回实际创建的怪物组
        /// <summary>
        /// 创建怪物
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static BaseUnit[] Create(int num) 
        {

            BaseUnit[] resout = new BaseUnit[num];

          //设计到线程问题导致每一个随机数都一样  需要进行线程
         
            //更具传入的值来创建对应的怪物
            for (int i = 0; i < num; i++)
            {
                BaseUnit monster = null;
                if (_deadMons.Count>0)
                {
                    monster = _deadMons.Dequeue();
                }
                else
                {
                    monster = new BaseUnit();
                }
                //随机怪物属性
                Random rd = new Random();
                StringBuilder builder = new StringBuilder();
                Thread.Sleep(10);
                int n =rd.Next(0,3);

                switch (n)
                {
                    case 0:
          
                        monster.MaxHP = monster.CurrentHp = 50;break;
                    case 1:
                        Thread.Sleep(10);
                        monster.MaxHP = monster.CurrentHp = (uint)(50 +rd.Next(7,13)); break;
                    case 2:
                        Thread.Sleep(10);
                        monster.MaxHP = monster.CurrentHp = (uint)(50 -rd.Next(2,10)); break;
                }
                builder.Append(HealthName[n]);
                Thread.Sleep(10);
                n = rd.Next(0,3);
                switch (n)
                {
                    case 0: monster.Attack  = 20; break;
                    case 1:
                        Thread.Sleep(10);
                        monster.Attack = (20 + rd.Next(2, 5)); break;
                    case 2:
                        Thread.Sleep(10);
                        monster.Attack = (20 - rd.Next(2, 6)); break;
                }
                builder.Append(AttackName[n]);
                Thread.Sleep(10);
                n = rd.Next(0, 3);
                switch (n)
                {
                    case 0: monster.Defence = 3; break;
                    case 1:
                        Thread.Sleep(10);
                        monster.Defence = (3 + rd.Next(1, 5)); break;
                    case 2:
                        Thread.Sleep(10);
                        monster.Defence = (3 - rd.Next(1, 2)); break;
                }
                builder.Append(DefenceName[n]);
                //随机名称
                builder.Append(Names[rd.Next(0,4)]);
                monster.Name = builder.ToString();
                //给怪物随机经验值 每个怪物等级也不一样
                monster.GetExp((uint)rd.Next(0, 1000));
                //将怪物复制到对应索引位置
                resout[i] = monster;
                //然后放入存活的怪物列表中
                LifeMons.Add(monster);
            }
            //最后返回
            return resout;
        }

        /// <summary>
        /// 随机获取一个怪物
        /// </summary>
        /// <param name="max">设置最大等级限制 如果小于或等于0就从所有怪物中查找</param>
        /// <returns></returns>
        public static BaseUnit GetRandomMonster(int maxLv = 0)
        {
            //本质上是从所有有怪物里面取随机索引进行获取
            //加上了限制
            Random rd = new Random();
            if (maxLv<=0)
            {
                int index = rd.Next(0, LifeMons.Count);
                return LifeMons[index];
            }
            else
            {
                //1 一直循环随机怪物 找到一个满足的返回
                //2 找到所有满足的怪物  再进行随机取值

                List<BaseUnit> temUnits = new List<BaseUnit>();
                //查找 使用lamba表达式
               temUnits= LifeMons.FindAll(m =>
                {
                    if (m.lever <= maxLv)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });

                int index=rd.Next(0, temUnits.Count);
                return temUnits[index];
            }
        }
        public static void SetMonsterDead(BaseUnit monster)
        {
            _deadMons.Enqueue(monster);//死亡组存入
            LifeMons.Remove(monster);//存活组移除
            monster.CurrentHp = 0;
        }
    }

}
